package com.google.demo.GPS;

import java.io.IOException;
import java.util.List;
import java.util.Locale;

import com.google.demo.R;
import com.google.demo.Util.Prefs;
import com.google.demo.Util.Util;
import com.google.demo.interfaces.AlertDialogInterface;
import com.google.demo.interfaces.ScanningGPS;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.location.Address;
import android.location.Geocoder;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.util.Log;


public class MyLocationManager implements LocationListener, AlertDialogInterface{
	private Context mContext;
	private ScanningGPS mCallback;
	private LocationManager mlocManager;
	private LocationListener mlocListener;
	private boolean network_enabled = false;
	private Prefs mPrefs;
	public MyLocationManager(Context context, ScanningGPS scanningGPS) {
		// TODO Auto-generated constructor stub
		mContext=context;
		mlocManager =(LocationManager)mContext.getSystemService(Context.LOCATION_SERVICE);
		mlocListener= this;
		mCallback=scanningGPS;
		mPrefs=new Prefs(mContext);
	}
	public void scanGPS(){
		try {	
			network_enabled = mlocManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
		} catch (Exception ex) {
		}
		
		// don't start listeners if no provider is enabled
		if (!network_enabled) {
			Util.showAlertDialog((Activity) mContext,
					this, true, true, mContext.getString(R.string.app_name),
					mContext.getString(R.string.message_alertdialog_nogps),
					mContext.getString(R.string.Yes),
					mContext.getString(R.string.No));
		} else if(mlocListener!=null){
			mlocManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, mlocListener);
		}
	}
	
	@Override
	public void onLocationChanged(Location location) {
		// TODO Auto-generated method stub
		if (mlocManager != null) {
			// This needs to stop getting the location data and save the battery power.
			double londitude =  location.getLongitude();
			double latitude =  	location.getLatitude();		
		    String address=getGeoCoderAddress(latitude, londitude);
		    if(address!=null){
		    	mPrefs.putStringValue(address, Prefs.KEY_ADDRESS_GPS);
				if(mlocListener!=null){
					Log.e("londitude", String.valueOf(londitude));
					Log.e("latitude", String.valueOf(latitude));
					mPrefs.putStringValue(String.valueOf(latitude), Prefs.KEY_LAT_GPS);
					mPrefs.putStringValue(String.valueOf(londitude), Prefs.KEY_LON_GPS);
					mCallback.onScanGPS(true, address);
					mlocManager.removeUpdates(mlocListener); 
					
				}
			    else{
			    	mCallback.onScanGPS(false,null);
			    }
		    	
		    }
		    
		}
	}

	@Override
	public void onProviderDisabled(String provider) {
		// TODO Auto-generated method stub
		
		Log.e("text", "Gps Disabled");
	}

	@Override
	public void onProviderEnabled(String provider) {
		// TODO Auto-generated method stub
		
		Log.e("Gps Enabled", "Gps Enabled");
	}

	@Override
	public void onStatusChanged(String provider, int status, Bundle extras) {
		// TODO Auto-generated method stub
		Log.e("status", String.valueOf(status));
	}
	
	public String getGeoCoderAddress(double lat,double lon){
		Geocoder geocoder=new Geocoder(mContext,Locale.getDefault());
		List<Address> addresses;
		String return_address="";
		try {
			addresses = geocoder.getFromLocation(lat, lon, 1);
			if(addresses!=null){
				if(addresses.size()>0){
					Address address=addresses.get(0);
					if(address.getAddressLine(0)!=null){
						return_address+=address.getAddressLine(0)+",";
					}if(address.getSubLocality()!=null){
						return_address+=address.getSubLocality()+",";
					}if(address.getSubAdminArea()!=null){
						return_address+=address.getSubAdminArea()+",";
					}if(address.getAdminArea()!=null){
						return_address+=address.getAdminArea();
					}
				}else
					return_address = mContext.getString(R.string.cannot_detect_gps);
			}else
				return_address = mContext.getString(R.string.cannot_detect_gps);
			
		} catch (IOException e) {			
			// TODO Auto-generated catch block
			Log.e("MyLocationManager", e.toString());
			return mContext.getString(R.string.cannot_detect_gps);
		} 
		return return_address;
	}
	
	@Override
	public void onChooseYesCallback() {
		// TODO Auto-generated method stub
		mCallback.isFirstOpenGPS(true);
		mContext.startActivity(new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS));
	}
	
	@Override
	public void onChooseNoCallback() {
		// TODO Auto-generated method stub
		mCallback.isFirstOpenGPS(false);
	}
}
